package com.bolt.core.view.components;

import java.util.ArrayList;
import java.util.List;

/**
 * 表单分组
 * Created by  on 2017/9/13.
 */
public abstract class GroupFormItem extends ClosingUI {

    private int column = 1;

    List <List <FormItem>> group = new ArrayList <List <FormItem>>();

    private boolean newRow = false;

    private List <FormItem> hiddenFormItems = new ArrayList <FormItem>();

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public List <List <FormItem>> getGroup() {
        return group;
    }

    public List <FormItem> getHiddenFormItems() {
        return hiddenFormItems;
    }


    /**
     * 表单分行分列处理
     * @param item　表单元素
     */
    public void populateGroupItem(FormItem item) {

       if (item.isHidden()) {
           hiddenFormItems.add(item);
        } else if (item.isSingleColumn()) {
           List<FormItem> row = new ArrayList <>();
           row.add(item);
           group.add(row);
           newRow = true;
        } else {
           if (group.size() == 0 || newRow) {
               group.add(new ArrayList <>());
           }
            if (group.get(group.size() - 1).size() >= getColumn()) {
                group.add(new ArrayList <>());
            }
            List <FormItem> lastRows = group.get(group.size() - 1);
            lastRows.add(item);
           newRow = false;
        }
    }

}
